package com.seed.core.query;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryTable;
import com.seed.core.column.Column;
import com.seed.core.enumeration.PropertyType;
import com.seed.core.enumeration.SqlOp;
import com.seed.core.pojo.Join;
import com.seed.core.pojo.seed.HasManys;
import com.seed.core.pojo.seed.Relation;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.pojo.Declared;
import com.seed.core.util.ConditionUtil;
import com.seed.util.StrKit;
import com.seed.util.SeedKit;

import java.util.*;

public class SeedKeyword {
    private final List<QueryCondition> wheres = new ArrayList<>(); // 关键字条件对象
    private final Map<String, Join> joins = new LinkedHashMap<>(); // 表关联对象
    private final Map<String, SeedInCondition> inQuerys = new LinkedHashMap<>();
    private final Map<String, Declared> fillKeywordeds = new HashMap<>(); // fill对象使用
    private final String keyword;

    public SeedKeyword(String keyword, Seed selfSeed, Map<String, Declared> declared) {
        this.keyword = keyword;
        init(null, selfSeed, declared);
    }

    private void init(String basePath, Seed selfSeed, Map<String, Declared> declared) {
        if(StrUtil.isBlank(keyword)) return;

        String selfTableAlias = StrKit.selfTableAlias(basePath);
        QueryTable selfTable = new QueryTable(selfSeed.getTableName()).as(selfTableAlias);
        for(Map.Entry<String, Declared> entry: declared.entrySet()) {
            String crossOrColumn = entry.getKey();
            String finalPath = StrKit.concat(basePath, crossOrColumn);
            Declared value = entry.getValue();
            PropertyType propertyType = value.getType();
            switch (propertyType) {
                case none -> {
                    Column nameColumn = SeedKit.getNameColumn(selfSeed);
//                    SqlOp op = SeedKit.op(selfSeed, nameColumn);
                    wheres.add(ConditionUtil.condition(selfTable, nameColumn.getName(), nameColumn.getDefaultOp().name(), keyword));
                }
                case asterisk -> {
                    Map<String, Column> columns = selfSeed.getColumns();
                    for (Column column: columns.values()) {
                        String columName = column.getName();
//                        SqlOp op = SeedKit.op(selfSeed, columName);
                        wheres.add(ConditionUtil.condition(selfTable, columName, column.getDefaultOp().name(), keyword));
                    }
                }
                case column -> {
//                    SqlOp op = SeedKit.op(selfSeed, crossOrColumn);
                    Column column = (Column) value.getProperty();
                    wheres.add(ConditionUtil.condition(selfTable, crossOrColumn, column.getDefaultOp().name(), keyword));
                }
                case hasOne, belongsTo -> {
                    Relation relation = (Relation) value.getProperty();
                    Seed targetSeed = relation.targetSeed();
                    String targetTableAlias = StrUtil.toCamelCase(finalPath);
                    QueryTable targetTable = new QueryTable(targetSeed.getTableName()).as(targetTableAlias);

                    QueryCondition on = relation.getQueryCondition(basePath, crossOrColumn);
                    joins.putIfAbsent(finalPath, new Join(targetTable, on));
                    init(finalPath, targetSeed, value);
                }
                default -> {
                    Relation relation = (Relation) value.getProperty();
                    Seed targetSeed = relation.targetSeed();
                    inQuerys.put(finalPath, new SeedInCondition(selfSeed, basePath, crossOrColumn, (HasManys) relation, targetSeed, keyword, value));
                    fillKeywordeds.put(finalPath, value);
                }
            }
        }
    }

    public String getKeyword() {
        return keyword;
    }

    public List<QueryCondition> getWheres() {
        return wheres;
    }

    public Map<String, Join> getJoins() {
        return joins;
    }

    public Map<String, SeedInCondition> getInQuerys() {
        return inQuerys;
    }

    public Declared getFillKeyworded(String key) {
        return fillKeywordeds.get(key);
    }
}
